
/*
 * @(#)ListFormReportAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.form.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.form.bean.FormGroup;
import com.dcivision.form.bean.FormRecord;
import com.dcivision.form.bean.FormSubmission;
import com.dcivision.form.dao.FormGroupDAObject;
import com.dcivision.form.dao.FormSubmissionDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;

/**
  ListFormReportAction.java

  This class is for list form report

    @author          Tony Chen
    @company         DCIVision Limited
    @creation date   16/01/2003
    @version         $Revision: 1.10.2.1 $
*/

public class ListFormReportAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.10.2.1 $";

  protected String listName = "";
  public static final String VAR_FORM_SUBMISSION_LIST = "listFormSubmission";
  public static final String VAR_ACTIVE_SUBMISSION_LIST = "listActiveSubmission";
  
  public ListFormReportAction() {
    super();
    this.setListName("releasedFormList");
    this.setListFunctionName("getNullListByReport");
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.form.dao.FormSubmissionDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.FORM_REPORT);
  }

  /**
   * setPageTitle
   * set the extend page title and page path.
   * default page path/title will be created by navmode and functionCode
   */
  public void setPageTitle(HttpServletRequest request, HttpServletResponse response, ActionForm form, ActionMapping mapping, ActionForward actionForward){
    FormGroup formGroup = (FormGroup)request.getAttribute("formGroup");
    if(!Utility.isEmpty(formGroup)){
      String extendTitle = formGroup.getGroupName();
      request.setAttribute(GlobalConstant.EXTEND_PAGE_TITLE, extendTitle);
      request.setAttribute(GlobalConstant.EXTEND_PAGE_TITLE_SHOW_ONLY, new Boolean(true));
    }
  }
  
  /**  Method getListData() - retrieve the appropriate item list for current page
  *
  *  @param      mapping               ActionMapping from struts
  *  @param      form                  ActionForm from struts
  *  @param      request               HttpServletReuqest
  *  @param      respond               HttpServletRespond
  *  @return     void
  *  @throws     ApplicationException  Throws Exception if cannot get the list data from DAO object
  */
  public void getListData(ActionMapping mapping,
                         AbstractSearchForm form,
                         HttpServletRequest request,
                         HttpServletResponse response)
     throws ApplicationException {
    super.getListData(mapping, form, request, response);
    
    ListFormRecordForm listFormRecordForm = (ListFormRecordForm)form;
    FormSubmissionDAObject dao = (FormSubmissionDAObject) this.getMajorDAObject(request);
    FormSubmissionDAObject formSubmissionDAO = (FormSubmissionDAObject) this.getDAObjectByClassName(
       request, "com.dcivision.form.dao.FormSubmissionDAObject");

    List formSubmissionList = new ArrayList(), noOfActiveRecordList = new ArrayList();
    List resultList = (List)request.getAttribute("releasedFormList");
       
    for (int i=0; i<resultList.size(); i++) {
      FormRecord formRecord = (FormRecord) resultList.get(i);
      Integer activeRecordCount = null;     
      activeRecordCount = formSubmissionDAO.getNoOfActiveRecordForReportByFormRecordID(formRecord.getID());     
      noOfActiveRecordList.add(activeRecordCount);
      if (activeRecordCount.intValue()>0) {
        FormSubmission formSubmission = (FormSubmission)formSubmissionDAO.getLastUpdateFormSubmissionByFormRecordID(formRecord.getID());          
        formSubmissionList.add(formSubmission);
      } else {
        formSubmissionList.add(new FormRecord());
      }
    }
    request.setAttribute(VAR_ACTIVE_SUBMISSION_LIST, noOfActiveRecordList);
    request.setAttribute(VAR_FORM_SUBMISSION_LIST, formSubmissionList);
    //Set Page Title 
    FormGroupDAObject formGroupDAO = new FormGroupDAObject(this.getSessionContainer(request),this.getConnection(request));
    FormGroup formGroup = (FormGroup)formGroupDAO.getObjectByID(listFormRecordForm.getFormGroupID());
    request.setAttribute("formGroup", formGroup);
  }

}
